package com.reader.api.service.impl.usercenter;

import com.alibaba.fastjson.JSONObject;
import com.reader.api.constants.RedisKeyConstants;
import com.reader.api.constants.SmsTemplateEnum;
import com.reader.api.exception.register.GetMobileCodeTooOftenException;
import com.reader.api.exception.register.IpGetMobileCodeTooOftenException;
import com.reader.api.exception.register.RegisterException;
import com.reader.api.service.usercenter.RegisterService;
import com.reader.api.thirdplatform.AliSmsService;
import com.reader.core.redis.RedisDao;
import com.reader.core.utils.IdUtils;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author liujianqing
 * @date 2018/10/24
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RedisDao redisDao;
    @Autowired
    private AliSmsService aliSmsService;


    @Override
    public boolean sendRegisterMobileCode(String mobile, String ip) {
        if(StringUtils.isBlank(mobile) || StringUtils.isBlank(ip)){
            return false;
        }

        //[1]验证码发送.
        String mobileCode = IdUtils.getRandDigit(4);
        JSONObject contentJson = new JSONObject();
        contentJson.put("code", mobileCode);
        boolean ret = aliSmsService.sendSmsCode(mobile, SmsTemplateEnum.LOGIN_TEMPLATE, contentJson);

        if (ret){
            //[2]存储验证码
            String mobileCodeKey = String.format(RedisKeyConstants.MOBILE_CODE_KEY,mobile);
            redisDao.save(mobileCodeKey,mobileCode,
                    RedisKeyConstants.MOBILE_CODE_EXPIRE_TIME, TimeUnit.SECONDS);

            //[3]存储1分钟发送标志
            String mobileCodeRecentSendKey = String.format(
                    RedisKeyConstants.MOBILE_CODE_RECENT_SEND_KEY,
                    mobile);
            redisDao.save(mobileCodeRecentSendKey,String.valueOf(1),
                    RedisKeyConstants.MOBILE_CODE_RECENT_SEND_TIME, TimeUnit.SECONDS);

            //[4]存储ip发送次数
            String mobileCodeIpCountKey = String.format(
                    RedisKeyConstants.MOBILE_CODE_IP_COUNT_KEY,
                    ip);
            redisDao.incr(mobileCodeIpCountKey);
            redisDao.setExpireTime(mobileCodeIpCountKey,
                    RedisKeyConstants.MOBILE_CODE_IP_COUNT_EXPIRE_TIME,
                    TimeUnit.SECONDS);
        }
        return ret;
    }

    @Override
    public boolean checkMobileAndIpCanSendCode(String mobile, String ip) throws RegisterException {
        if(StringUtils.isBlank(mobile) || StringUtils.isBlank(ip)){
            return false;
        }

        //[1]1分钟内是否已经发过
        String mobileCodeRecentSendKey = String.format(
                RedisKeyConstants.MOBILE_CODE_RECENT_SEND_KEY,
                mobile);
        if (redisDao.existKey(mobileCodeRecentSendKey)){
            throw  new GetMobileCodeTooOftenException();
        }

        //[2]ip防刷check
        String mobileCodeIpCountKey = String.format(
                RedisKeyConstants.MOBILE_CODE_IP_COUNT_KEY,
                ip);
        String value = redisDao.get(mobileCodeIpCountKey);
        int count = Integer.parseInt(value);
        if(count>RedisKeyConstants.MOBILE_CODE_IP_COUNT_MAX_VALUE){
            throw  new IpGetMobileCodeTooOftenException();
        }

        return true;
    }

    @Override
    public boolean isRightMobileCode(String mobile, String mobileCode) {
        if(StringUtils.isBlank(mobile) || StringUtils.isBlank(mobileCode)){
            return false;
        }

        String mobileCodeKey = String.format(RedisKeyConstants.MOBILE_CODE_KEY,mobile);
        String value = redisDao.get(mobileCodeKey);

        if(mobileCode.equals(value)){
            return true;
        }
        return false;
    }
}
